package com.yiwei.zxp.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.Spanned;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.yiwei.zxp.R;
import com.yiwei.zxp.activity.CommissionWithdrawDetailActivity;
import com.yiwei.zxp.api.model.CommissionListModel;
import com.yiwei.zxp.util.DateTime;
import com.yiwei.zxp.util.Spans;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class CommissionDetailAdapter extends ItemAdapter<CommissionListModel.DatasBean, CommissionDetailAdapter.DefaultHolder> {


    private final int x32;
    private final int x24;

    private Context context;

    public CommissionDetailAdapter(Context context) {
        super(context);
        this.context = context;
        x32 = context.getResources().getDimensionPixelSize(R.dimen.x32);
        x24 = context.getResources().getDimensionPixelSize(R.dimen.x24);
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_commission_detail, parent));
    }

    // "status": "2",                  //1：收入佣金，2：提现
    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        CommissionListModel.DatasBean item = getItem(position);
        Spanned txt_status = Spans.with(context).font(getStatus(item.status), x32, 0xff333333).
                font("\n" + DateTime.toDateTime(item.created_at * 1000), x24, 0xff666666).
                build();
        holder.txtContent.setText(txt_status);

        if (item.status.equals("1")) {
            holder.txtMoney.setText("+" + item.commission);
            holder.txtMoney.setTextColor(0xff31AA27);
        } else if (item.status.equals("2")) {
            holder.txtMoney.setText("-" + item.commission);
            holder.txtMoney.setTextColor(0xffFF0030);
        }
        holder.layoutTotal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, CommissionWithdrawDetailActivity.class);
                intent.putExtra("id", item.id);
                intent.putExtra("status", item.status);
                context.startActivity(intent);
                ((Activity) context).overridePendingTransition(R.anim.right_enter, R.anim.left_out);
            }
        });
    }

    private String getStatus(String status) {
        switch (status) {
            case "1":
                return "佣金到账";
            case "2":
                return "佣金提现";
            default:
                return "";
        }
    }


    class DefaultHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.txt_content)
        TextView txtContent;
        @BindView(R.id.txt_money)
        TextView txtMoney;
        @BindView(R.id.layout_total)
        FrameLayout layoutTotal;

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
